/**
 * 我的手牌
 */
class MyHand extends eui.Component {
	public handGroup: eui.Group;
	public receiveCard: MyHandCard;

	public peng_0: Peng;
	public peng_1: Peng;
	public peng_2: Peng;
	public peng_3: Peng;

	public handCards: Array<number> = [];
	public pengList: Array<PengItem> = [];
	public cardNum = 0;
	public constructor() {
		super();
		this.skinName = "MyHandSkin";
	}

	public childrenCreated(): void {
		/*this.addEvent({
			onTing: ActionPanel.ONTING,
			onGameStart: CmdConfig.GameStart,
			onUserOutCard: CmdConfig.GetUserOutCard
		});*/
	}
	//游戏开始
	public onGameStart(): void {
		for (var i = 0; i < this.handGroup.numChildren; i++) {
			var card = (this.handGroup.getChildByName(i.toString()) as MyHandCard);
			card.card.hight = true;
		}
		this.receiveCard.card.hight = true;
	}

	//用户出牌
	public onUserOutCard(d: any): void {
		for (var i = 0; i < this.handGroup.numChildren; i++) {
			var card = (this.handGroup.getChildByName(i.toString()) as MyHandCard);
			card.card.hight = false;
		}
		this.receiveCard.card.hight = true;
	}

	//用户听牌
	public onTing(d: any): void {
		for (var i = 0; i < this.handGroup.numChildren; i++) {
			var card = (this.handGroup.getChildByName(i.toString()) as MyHandCard);
			card.card.hight = d.indexOf(card.value) != -1;
		}
		this.receiveCard.card.hight = d.indexOf(this.receiveCard.value) != -1;
	}
	/**
	 * 设置手牌下降
	 */
	public allCardDown(): void {
		this.receiveCard.bottom = 0;
		this.receiveCard.beenTouched = false;
		for (var i = 0; i < this.handGroup.numChildren; i++) {
			var card = (this.handGroup.getChildByName(i.toString()) as MyHandCard);
			card.bottom = 0;
			card.beenTouched = false;
		}
	}
	/**设置手牌数据 */
	public setData(cards: Array<number> | number): void {
		if (typeof (cards) == "number") {
			this.cardNum = cards;
			if (this.cardNum == 14) {
				this.receiveHandCard(-1);
				this.cardNum--;
			}
			for (let i = 0; i < 13; i++) {
				if (i < this.cardNum) {
					(this.handGroup.getChildByName(i.toString()) as MyHandCard).setData(-1);
				} else {
					(this.handGroup.getChildByName(i.toString()) as MyHandCard).visible = false;
				}
			}
		} else {
			var rev = 0;
			if (cards.length == 14) {
				rev = cards.pop();
			}
			this.handCards = cards;
			this.handCards.sort(function (a, b) { return a >= b ? -1 : 1 });
			for (var i = 0; i < 13; i++) {
				if (this.handCards[i]) {
					(this.handGroup.getChildByName(i.toString()) as MyHandCard).setData(this.handCards[i]);
				} else {
					(this.handGroup.getChildByName(i.toString()) as MyHandCard).visible = false;
				}
			}
			if (rev != 0) {
				this.receiveHandCard(rev);
			}
		}
	}
	/**失去手牌 */
	public loseCard(value: number): void {
		var index = this.handCards.indexOf(value);
		this.handCards.splice(index, 1);
		this.setData(this.handCards);
	}
	/**收到手牌 */
	public receiveHandCard(value: number): void {
		if (value > 0) {
			this.receiveCard.setData(value);
			this.receiveCard.visible = true;
			this.handCards.push(value);
		} else {
			this.cardNum++;
			this.receiveCard.setData(-1);
			this.receiveCard.visible = true;
		}
	}
	/**隐藏收到手牌 */
	public hideReceiveCard(): void {
		this.receiveCard.card.source = "";
		this.receiveCard.visible = false;
	}

	/**玩家吃碰杠 {"wOperateUser":1,"cbOperateCard":34,"cbOperateCode":16,"wProvideUser":2}*/
	public pengGang(d: any, isReConnect = false): void {
		this.hideReceiveCard();
		var data = [];
		switch (d.cbOperateCode) {
			case MjLogic.Prio.Peng:
				data = [d.cbOperateCard, d.cbOperateCard, d.cbOperateCard];
				if (!isReConnect) { this.handCards.push(d.cbOperateCard) };
				break;
			case MjLogic.Prio.ChiLeft:
				data = [d.cbOperateCard, d.cbOperateCard + 1, d.cbOperateCard + 2];
				if (!isReConnect) { this.handCards.push(d.cbOperateCard) };
				break;
			case MjLogic.Prio.ChiMiddle:
				data = [d.cbOperateCard - 1, d.cbOperateCard, d.cbOperateCard + 1];
				if (!isReConnect) { this.handCards.push(d.cbOperateCard) };
				break;
			case MjLogic.Prio.ChiRight:
				data = [d.cbOperateCard - 2, d.cbOperateCard - 1, d.cbOperateCard];
				if (!isReConnect) { this.handCards.push(d.cbOperateCard) };
				break;
			case MjLogic.Prio.Gang:
				data = [d.cbOperateCard, d.cbOperateCard, d.cbOperateCard, d.cbOperateCard];
				if (!isReConnect) { this.handCards.push(d.cbOperateCard) };
				break;
		}
		this.pengList.push(new PengItem(data, d.cbOperateCode, d.wProvideUser));
		if (d.cbOperateCode != MjLogic.Prio.Gang) {
			(this["peng_" + (this.pengList.length - 1)] as Peng).setData(this.pengList[this.pengList.length - 1]);
			if (!isReConnect) { this.deletePeng(this.pengList[this.pengList.length - 1]); }
		} else {
			for (let i = 0; i < this.pengList.length; i++) {
				if (this.pengList[i].type == MjLogic.Prio.Peng && this.pengList[i].data[0] == data[0]) {
					(this["peng_" + i.toString()] as Peng).setBuGang();
					this.pengList[i].type = MjLogic.Prio.Gang;
					if (!isReConnect) { this.deletePeng(this.pengList[i]); }
				}
			}
			if (isReConnect) {
				this.pengList.push(new PengItem(data, d.cbOperateCode, d.wProvideUser));
				(this["peng_" + (this.pengList.length - 1)] as Peng).setData(this.pengList[this.pengList.length - 1]);
			}
		}
	}

	public deletePeng(d: PengItem): void {
		for (let i = 0; i < d.data.length; i++) {
			var index = this.handCards.indexOf(d.data[i]);
			if (index >= 0) {
				this.handCards.splice(index, 1);
			}
		}
		this.setData(this.handCards);
	}

	public clear(): void {
		for (let j = 0; j < 13; j++) {
			this.handGroup.getChildByName(j.toString()).visible = true;
		}
		for (var i = 0; i < 4; i++) {
			this["peng_" + i].clear();
		}
		this.visible = false;
		this.hideReceiveCard();
		this.pengList = [];
	}
}